package com.example.nio.stream;

import static java.util.stream.Collectors.toList;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ReduceExample {

    public static void main(String[] args) {Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario","Milan");
        Trader alan = new Trader("Alan","Cambridge");
        Trader brian = new Trader("Brian","Cambridge");
        List<Transaction> transactions = Arrays.asList(
            new Transaction(brian, 2011, 300),
            new Transaction(raoul, 2012, 1000),
            new Transaction(raoul, 2011, 400),
            new Transaction(mario, 2012, 710),
            new Transaction(mario, 2012, 700),
            new Transaction(alan, 2012, 950)
        );

        List<Transaction> t1 = transactions.stream()
            .filter((transaction)->transaction.getYear()==2011)
            .sorted(Comparator.comparing(Transaction::getValue)).collect(
            toList());
        System.out.println(t1);

        List<String> t2 = transactions.stream()
            .map((t)->t.getTrader().getCity()).distinct().collect(toList());
        System.out.println(t2);

        List<Trader> t3 = transactions.stream()
            .map(Transaction::getTrader)
            .filter((t)->t.getCity().equals("Cambridge"))
            .sorted(Comparator.comparing(Trader::getName)).collect(Collectors.toList());
        System.out.println(t3);
    }

}
